package com.leo.foodmaster.admin.purchase.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.foodMaterial.entity.FoodMaterial;
import com.leo.foodmaster.admin.purchase.entity.PurchaseBill;
import com.leo.foodmaster.admin.purchase.entity.PurchaseBillDetails;
import com.leo.foodmaster.admin.purchase.service.IPurchaseBillDetailsService;
import com.leo.foodmaster.admin.purchase.service.IPurchaseBillService;
import com.leo.foodmaster.admin.system.entity.SysUser;
import com.leo.foodmaster.admin.system.service.IUserService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.enums.StatusEnum;
import com.leo.foodmaster.commons.util.ExcelUtil;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购单 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-08-01
 */
@RestController
@Api(tags = "采购单管理")
@AllArgsConstructor
@RequestMapping("/purchase/bill")
public class PurchaseBillController {
    private final IPurchaseBillService purchaseBillService;
    private final IPurchaseBillDetailsService purchaseBillDetailsService;
    private final IUserService userService;


    @ApiOperation("条件分页查询采购单")
    @GetMapping("/list")
    public Result<IPage<PurchaseBill>> getPurchaseBillPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "采购单编号") @RequestParam(required = false) String serialNum,
            @ApiParam(value = "供应商Id") @RequestParam(required = false) Long supplierId,
            @ApiParam(value = "审核状态") @RequestParam(required = false) List<String> statusList,
            @ApiParam(value = "付款方式") @RequestParam(required = false) String paymentType,
            @ApiParam(value = "支付状态") @RequestParam(required = false) String payStatus,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime
    ) {
        IPage<PurchaseBill> pageList  = purchaseBillService.getPurchaseBillPageList(new Page<>(pageNum, pageSize), serialNum,
                supplierId, statusList, paymentType, payStatus, beginTime, endTime);
        return Result.success(pageList);
    }

    @GetMapping("userList")
    @ApiOperation("查询机构用户列表")
    public Result<List<SysUser>> getUserPageList() {
        Long organId = WebUtils.getUser().getOrganId();
        List<SysUser> list = userService.list(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getStatus, StatusEnum.ENABLED.getCode())
                .eq(SysUser::getOrganId, organId));
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增采购单")
    public Result<Object> addPurchaseBill(@RequestBody PurchaseBill purchaseBill) {
        return purchaseBillService.addPurchaseBill(purchaseBill);
    }

    @PutMapping
    @ApiOperation("修改采购单")
    public Result<Object> updatePurchaseBill(@RequestBody PurchaseBill purchaseBill) {
        return purchaseBillService.updatePurchaseBill(purchaseBill);
    }

    @PutMapping("/check")
    @ApiOperation("审核采购单")
    public Result<Object> checkPurchaseBill(@RequestBody PurchaseBill purchaseBill) {
        return purchaseBillService.checkPurchaseBill(purchaseBill);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除采购单")
    public Result<Object> deletePurchaseBill(@ApiParam("食材ids") @PathVariable Long id) {
        return purchaseBillService.deletePurchaseBill(id);
    }

    @ApiOperation("根据id查询采购清单详情")
    @GetMapping("/{id}")
    public Result<PurchaseBill> getById(@ApiParam("采购清单Id") @PathVariable Long id){
        PurchaseBill purchaseBill = purchaseBillService.getById(id);
        List<PurchaseBillDetails> list = purchaseBillDetailsService.list(Wrappers.lambdaQuery(PurchaseBillDetails.class)
                .eq(PurchaseBillDetails::getPurchaseBillId, id));
        purchaseBill.setDetailList(list);
        return Result.success(purchaseBill);
    }

    @GetMapping("/export")
    @ApiOperation("导出采购数据")
    public Result<Object> export(@ApiParam(value = "采购单编号") @RequestParam(required = false) String serialNum,
                                 @ApiParam(value = "供应商Id") @RequestParam(required = false) Long supplierId,
                                 @ApiParam(value = "审核状态") @RequestParam(required = false) List<String> statusList,
                                 @ApiParam(value = "付款方式") @RequestParam(required = false) String paymentType,
                                 @ApiParam(value = "支付状态") @RequestParam(required = false) String payStatus,
                                 @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                                 @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime) {
        List<PurchaseBill> list  = purchaseBillService
                .getFoodMaterialList(serialNum, supplierId, statusList, paymentType, payStatus, beginTime, endTime);
        ExcelUtil<PurchaseBill> util = new ExcelUtil<>(PurchaseBill.class);
        return util.exportExcel(list, "采购清单数据");
    }

}

